package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NetStatusEvent;
	import flash.net.GroupSpecifier;
	import flash.net.NetConnection;
	import flash.net.NetGroup;
	
	import fla.view.P2PView;

	public class RTMFPGroups extends Sprite
	{
		// Represents a network connection.
		private var netConnection:NetConnection;

		private var connected:Boolean;

		private var netGroup:NetGroup;

		private var sequenceNumber:int;
		private var connectSuccess:Boolean = false;
		private var joinedGroup:Boolean=false;

		private var view:P2PView = new P2PView();
		private var groupNameText:Object;
		private var chatText:Object;
		private var userNameText:Object;
		
		public function RTMFPGroups()
		{
			addChild(view);
			view.joinGroupBtn.addEventListener(MouseEvent.CLICK,OnConnect);
			view.sendBtn.addEventListener(MouseEvent.CLICK,DoPost);
			
			groupNameText = view.groupNameText;
			userNameText = view.userNameText;
			chatText = view.chatText;
			
			netConnection = new NetConnection();

			// Connect to the server.
			netConnection.connect("rtmfp://localhost/test");

			netConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
		}

		private function onNetStatusHandler(evt:NetStatusEvent):void
		{
			logger(evt.info.code+"\n");
			switch(evt.info.code)
			{
				case "NetConnection.Connect.Success":
				{
					connectSuccess = true;
					break;
				}
				case "NetGroup.Connect.Success":
				{
					joinedGroup = true;
					break;
				}
					
				default:
				{
					break;
				}
			}
		}

		private function OnConnect(event:Event):void
		{
			logger("Connected\n");
			connected = true;
			// Create a GroupSpecifier object to pass to the NetGroup constructor. 
			// The GroupSpecifier determines the properties of the group 
			var groupSpecifier:GroupSpecifier;
			groupSpecifier = new GroupSpecifier("com.aslrexample/" + groupNameText.text);
			groupSpecifier.postingEnabled = true;
			groupSpecifier.serverChannelEnabled = true;
			
			netGroup = new NetGroup(netConnection, groupSpecifier.groupspecWithAuthorizations());
			netGroup.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler);
			logger("Join \"" + groupSpecifier.groupspecWithAuthorizations() + "\"\n");
			
		}

		// Called when you the chatText field has focus and you press Enter. 
		private function DoPost(e:Event):void
		{
			if (joinedGroup)
			{
				// Build the message to post. 
				var message:Object = new Object;
				message.user = userNameText.text;
				message.text = chatText.text;
				message.sequence = sequenceNumber++;
				message.sender = netConnection.nearID;
				// Post the message to the group. 
				netGroup.post(message);
				logger("==> " + chatText.text + "\n")
			}
			else
			{
				logger("Click Connect before sending a chat message");
			}
			chatText.text = "";
		}

		public function logger(info:String):void
		{
			view.messageTxt.appendText(info);
		}
		
	}
}
